< Summary

Class:GDX.ArrayExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/ArrayExtensions.cs
Covered lines:73
Uncovered lines:20
Coverable lines:93
Total lines:225
Line coverage:78.4% (73 of 93)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:10
Method coverage:80% (8 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Clear[T]()0%110100%
ContainsItem[T](...)0%3.013091.67%
ContainsReference[T](...)0%3.013091.67%
ContainsValue[T](...)0%3.013091.67%
FirstIndexOf[T](...)0%12300%
FirstIndexOfItem[T](...)0%3.013091.67%
FirstIndexOfValue[T](...)0%3.013091.67%
LastIndexOf[T](...)0%12300%
LastIndexOfItem[T](...)0%3.043083.33%
LastIndexOfValue[T](...)0%3.013091.67%

File(s)

./Packages/com.dotbunny.gdx/GDX/ArrayExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Runtime.CompilerServices;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     Array Based Extension Methods
 12    /// </summary>
 13    [VisualScriptingCompatible(2)]
 14    public static class ArrayExtensions
 15    {
 16        /// <summary>
 17        ///     Set all elements in an array to the default values.
 18        /// </summary>
 19        /// <remarks>
 20        ///     This does not alter the <paramref name="targetArray"/>'s length.
 21        /// </remarks>
 22        /// <param name="targetArray">The array to be defaulted.</param>
 23        /// <typeparam name="T">The type of the array.</typeparam>
 24        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 25        public static void Clear<T>(this T[] targetArray)
 126        {
 127            Array.Clear(targetArray, 0, targetArray.Length);
 128        }
 29
 30        /// <summary>
 31        ///     <para>Does <paramref name="targetArray" /> contain <paramref name="targetValue" />?</para>
 32        /// </summary>
 33        /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param>
 34        /// <param name="targetValue">The target item to look for.</param>
 35        /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam>
 36        /// <returns>true/false</returns>
 37        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 38        public static bool ContainsItem<T>(this T[] targetArray, T targetValue) where T : class
 339        {
 340            int count = targetArray.Length;
 2041            for (int i = 0; i < count; i++)
 942            {
 943                if (targetArray[i] == targetValue)
 244                {
 245                    return true;
 46                }
 747            }
 148            return false;
 349        }
 50
 51        /// <summary>
 52        ///     <para>Does <paramref name="targetArray" /> contain <paramref name="targetItem" />?</para>
 53        /// </summary>
 54        /// <remarks>Ignores equality check and end up comparing object pointers.</remarks>
 55        /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param>
 56        /// <param name="targetItem">The target item to look for.</param>
 57        /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam>
 58        /// <returns>true/false</returns>
 59        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60        public static bool ContainsReference<T>(this T[] targetArray, T targetItem) where T : class
 261        {
 262            int count = targetArray.Length;
 1263            for (int i = 0; i < count; i++)
 564            {
 65#pragma warning disable
 66                // ReSharper disable All
 567                if ((System.Object)targetArray[i] == (System.Object)targetItem)
 168                {
 169                    return true;
 70                }
 71                // ReSharper restore All
 72#pragma warning restore
 473            }
 174            return false;
 275        }
 76
 77        /// <summary>
 78        ///     <para>Does <paramref name="targetArray" /> contain <paramref name="targetValue" />?</para>
 79        /// </summary>
 80        /// <param name="targetArray">The <see cref="System.Array" /> to look in.</param>
 81        /// <param name="targetValue">The target value to look for.</param>
 82        /// <typeparam name="T">The type of the <see cref="System.Array" />.</typeparam>
 83        /// <returns>true/false</returns>
 84        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 85        public static bool ContainsValue<T>(this T[] targetArray, T targetValue) where T : IEquatable<T>
 286        {
 287            int count = targetArray.Length;
 1888            for (int i = 0; i < count; i++)
 889            {
 890                if (targetArray[i].Equals(targetValue))
 191                {
 192                    return true;
 93                }
 794            }
 195            return false;
 296        }
 97
 98        /// <summary>
 99        ///     Find the first index of <paramref name="targetItem" /> in <paramref name="targetArray" />.
 100        /// </summary>
 101        /// <remarks>This will work for <see cref="string"/> comparisons.</remarks>
 102        /// <param name="targetArray">The array which to look in.</param>
 103        /// <param name="targetItem">The object to be found.</param>
 104        /// <typeparam name="T">The type of the array.</typeparam>
 105        /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found.
 106        public static int FirstIndexOf<T>(this T[] targetArray, T targetItem) where T : IEquatable<T>
 0107        {
 0108            int length = targetArray.Length;
 0109            for (int i = 0; i < length; i++)
 0110            {
 0111                if (targetArray[i].Equals(targetItem))
 0112                {
 0113                    return i;
 114                }
 0115            }
 0116            return -1;
 0117        }
 118
 119        /// <summary>
 120        ///     Find the first index of <paramref name="targetItem" /> in <paramref name="targetArray" />.
 121        /// </summary>
 122        /// <remarks>Ignores equality check and end up comparing object pointers. Do NOT use this for <see cref="string"
 123        /// <param name="targetArray">The array which to look in.</param>
 124        /// <param name="targetItem">The object to be found.</param>
 125        /// <typeparam name="T">The type of the array.</typeparam>
 126        /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found.
 127        public static int FirstIndexOfItem<T>(this T[] targetArray, T targetItem) where T : class
 2128        {
 2129            int length = targetArray.Length;
 14130            for (int i = 0; i < length; i++)
 6131            {
 6132                if (targetArray[i] == targetItem)
 1133                {
 1134                    return i;
 135                }
 5136            }
 1137            return -1;
 2138        }
 139
 140        /// <summary>
 141        ///     Find the first index of <paramref name="targetValue" /> in <paramref name="targetArray" />.
 142        /// </summary>
 143        /// <param name="targetArray">The array which to look in.</param>
 144        /// <param name="targetValue">The value to be found.</param>
 145        /// <typeparam name="T">The type of the array.</typeparam>
 146        /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetArray" />, or -1 if not found
 147        public static int FirstIndexOfValue<T>(this T[] targetArray, T targetValue) where T : struct
 2148        {
 2149            int length = targetArray.Length;
 32150            for (int i = 0; i < length; i++)
 15151            {
 15152                if (targetArray[i].Equals(targetValue))
 1153                {
 1154                    return i;
 155                }
 14156            }
 157
 1158            return -1;
 2159        }
 160
 161        /// <summary>
 162        ///     Find the last index of <paramref name="targetItem" /> in <paramref name="targetArray" />.
 163        /// </summary>
 164        /// <remarks>This will work for <see cref="string"/> comparisons.</remarks>
 165        /// <param name="targetArray">The array which to look in.</param>
 166        /// <param name="targetItem">The object to be found.</param>
 167        /// <typeparam name="T">The type of the array.</typeparam>
 168        /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found.
 169        public static int LastIndexOf<T>(this T[] targetArray, T targetItem) where T : IEquatable<T>
 0170        {
 0171            int length = targetArray.Length;
 0172            for (int i = length - 1; i >= 0; i--)
 0173            {
 0174                if (targetArray[i].Equals(targetItem))
 0175                {
 0176                    return i;
 177                }
 0178            }
 179
 0180            return -1;
 0181        }
 182
 183        /// <summary>
 184        ///     Find the last index of <paramref name="targetItem" /> in <paramref name="targetArray" />.
 185        /// </summary>
 186        /// <param name="targetArray">The array which to look in.</param>
 187        /// <param name="targetItem">The object to be found.</param>
 188        /// <typeparam name="T">The type of the array.</typeparam>
 189        /// <returns>The index of <paramref name="targetItem" /> in <paramref name="targetArray" />, or -1 if not found.
 190        public static int LastIndexOfItem<T>(this T[] targetArray, T targetItem) where T : class
 2191        {
 2192            int length = targetArray.Length;
 12193            for (int i = length - 1; i >= 0; i--)
 5194            {
 5195                if (targetArray[i] == targetItem)
 1196                {
 1197                    return i;
 198                }
 4199            }
 200
 1201            return -1;
 2202        }
 203
 204        /// <summary>
 205        ///     Find the last index of <paramref name="targetValue" /> in <paramref name="targetArray" />.
 206        /// </summary>
 207        /// <param name="targetArray">The array which to look in.</param>
 208        /// <param name="targetValue">The value to be found.</param>
 209        /// <typeparam name="T">The type of the array.</typeparam>
 210        /// <returns>The index of <paramref name="targetValue" /> in <paramref name="targetArray" />, or -1 if not found
 211        public static int LastIndexOfValue<T>(this T[] targetArray, T targetValue) where T : struct
 2212        {
 2213            int length = targetArray.Length;
 32214            for (int i = length - 1; i >= 0; i--)
 15215            {
 15216                if (targetArray[i].Equals(targetValue))
 1217                {
 1218                    return i;
 219                }
 14220            }
 221
 1222            return -1;
 2223        }
 224    }
 225}

Coverage by test methods














































































































































































































































































































































































































































































































































































































































































































Methods/Properties

Clear[T]()
ContainsItem[T](, T)
ContainsReference[T](, T)
ContainsValue[T](, T)
FirstIndexOf[T](, T)
FirstIndexOfItem[T](, T)
FirstIndexOfValue[T](, T)
LastIndexOf[T](, T)
LastIndexOfItem[T](, T)
LastIndexOfValue[T](, T)